﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using School.Entities;
using System.Drawing;

namespace School.Pages.BOOKS
{
    public partial class SRV_BOOKS_RECIEVING : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var x = (IQueryable<int>)Session["privilage"];

                if (x.Contains(1108) == true)
                {

                    DropDownList ddl = ((DropDownList)(CampusPackage1.FindControl("ddlGrade")));
                    ddl.AutoPostBack = true;
                    ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);
                    lblResult.Text = "";
                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");
            }
            else
                Response.Redirect("../organization/no_privelege.aspx");
        }

        void ddl_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList Gradeddl = (DropDownList)sender;
            if (Gradeddl.SelectedIndex != 0)
            {
                if (CampusPackage1.CampusId != "0" && CampusPackage1.CertificateId != "0" && CampusPackage1.GradeId != "0" && CampusPackage1.StageId != "0")
                {
                    gvData.DataSource = School.Service.BOOKS.SRV_BOOKS.AllBooks().Where(s => s.Campus_Id == long.Parse(CampusPackage1.CampusId) &&
                        s.Cer_Id == long.Parse(CampusPackage1.CertificateId) && s.Grad_Id == long.Parse(CampusPackage1.GradeId) && s.Stage_Id == long.Parse(CampusPackage1.StageId));
                    gvData.DataBind();
                }
            }
        }
        protected void btnSaveChanges_Click(object sender, EventArgs e)
        {
            List<SRV_BOOK_TRANSACTION> lsttrans = new List<SRV_BOOK_TRANSACTION>();
            TextBox txtQty;
            if (gvData.Rows.Count > 0)
            {
                foreach (GridViewRow item in gvData.Rows)
                {
                    if (item.RowType == DataControlRowType.DataRow)
                    {
                        txtQty = (TextBox)item.Cells[2].FindControl("txtQty");
                        lsttrans.Add(new SRV_BOOK_TRANSACTION()
                        {
                            Book_Id = int.Parse(gvData.DataKeys[item.RowIndex].Values["Id"].ToString()),
                            Date_Time = DateTime.Now,
                            Qty = int.Parse(txtQty.Text),
                            Transaction_Type = 1
                        });
                    }
                }
                if (School.Service.BOOKS.SRV_BOOKS_TRANSACTIONS.SRV_BOOKS_TRANACTIONSInsert(lsttrans))
                {
                    lblResult.ForeColor = Color.Green;
                    lblResult.Text = "تمت الاضافه بنجاح";
                    ClearGrid();
                }
            }
            else
            {
                lblResult.ForeColor = Color.Red;
                lblResult.Text = "لم تتم عمليه الاضافه";
            }

        }
        public void ClearGrid()
        {
            foreach (GridViewRow item in gvData.Rows)
            {
                if (item.RowType == DataControlRowType.DataRow)
                {
                    TextBox txtQty = (TextBox)item.Cells[2].FindControl("txtQty");
                    txtQty.Text = "";
                }
            }

        }
    }
}
